import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/recipe/data/api/recipe_today_api.dart';
import 'package:casdon/modules/recipe/data/model/recipe_today_model.dart';
import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';

class RecipeTodayListUsecase {
  final RecipeRepository _repository;

  const RecipeTodayListUsecase(this._repository);

  Future<ResponseRecipeTodayApi> execute({
    required int pageNum,
    required int pageSize,
    TodayRecipeSortType? sortType,
  }) async {
    try {
      sortType = sortType ?? TodayRecipeSortType.view;
      var ret = await _repository.getRecipeTodayList(
        pageNum,
        pageSize,
        sortType.name,
      );
      ret ??= ResponseRecipeTodayApi(total: 0, rows: []);
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("RecipeTodayListUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("RecipeTodayListUsecase 失败");
    }
  }
}
